package com.fau.wmbj.server.audit;


import com.fau.wmbj.server.entity.AuditLog;
import com.fau.wmbj.server.utils.WebUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * 审计日志拦截器-Audit
 *
 * @author jiawei
 */
public class AuditLogInterceptor implements HandlerInterceptor {


    /**
     * 请求前处理
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param handler  处理器
     * @return 是否继续执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Audit audit = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Audit.class);
            // UserBaseService userService = (UserBaseService) SpringUtils.getBean("userBaseServiceImpl");
            if (audit != null) {
                AuditLog auditLog = new AuditLog();
                auditLog.setAction(audit.action());
                auditLog.setIp(WebUtils.getIpAddr(request));
                auditLog.setRequestUrl(request.getRequestURL().toString());
                auditLog.setParameters(new HashMap<>(request.getParameterMap()));
                // UserBase current = userService.getCurrent();
                // if (current != null) {
                //     if (current instanceof User) {
                //         User admin = (User) current;
                //         StringBuilder sb = new StringBuilder();
                //         sb.append(admin.getUsername()).append(",").append(admin.getName()).append(",").
                //                 append(admin.getEmail()).append(",").append(admin.getMobile());
                //         auditLog.setUserInfo(sb.toString());
                //         request.setAttribute(AuditLog.AUDIT_LOG_ATTRIBUTE_NAME, auditLog);
                //     }
                // }

            }
        }

        return true;
    }

    /**
     * 请求后处理
     *
     * @param request      HttpServletRequest
     * @param response     HttpServletResponse
     * @param handler      处理器
     * @param modelAndView 数据视图
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            // AuditLogService auditLogService = (AuditLogService) SpringUtils.getBean("auditLogServiceImpl");

            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Audit audit = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Audit.class);
            if (audit != null) {
                AuditLog auditLog = (AuditLog) request.getAttribute(AuditLog.AUDIT_LOG_ATTRIBUTE_NAME);
                if (auditLog != null && auditLog.isNew()) {
                    // auditLogService.create(auditLog);
                }
            }
        }
    }

}